package com.framework.modules.chat.dao;

import com.framework.modules.chat.entity.ChatFriendEntity;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.framework.modules.dataview.entity.StatLikeAndAttentUserEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 聊天联系人 
 * 
 * @author MIT
 * @email litmiss@gmail.com
 * @date 2020-10-31 02:06:49
 */
public interface ChatFriendDao extends BaseMapper<ChatFriendEntity> {

    @Update("update tb_chat_friend set block_message=#{blockMessage},block_count = block_count + 1,block_date=unix_timestamp(now()) where uid=#{toUid} and target_uid=#{fromUid};" +
            "update tb_chat_friend set block_message=#{blockMessage},block_count =0 ,block_date=unix_timestamp(now()) where uid=#{fromUid} and target_uid=#{toUid};")
    Integer updateFriendBlockMessage(@Param("toUid")Integer toUid,@Param("fromUid")Integer fromUid,@Param("blockMessage")String blockMessage);

    @Update("update tb_chat_friend set state=#{state} where uid=#{uid} and target_uid=#{targetUid};" +
            "update tb_chat_friend set state=#{state} where uid=#{targetUid} and target_uid=#{uid};")
    Integer updateFriendState(@Param("uid")Integer uid,@Param("targetUid")Integer targetUid,@Param("state")Integer state);

    Integer saveOrUpdateBatch(@Param("list") List<ChatFriendEntity> list);

    @Select("SELECT (SELECT count(1) from tb_chat_friend  where uid=#{uid} and state=1) as my_attent_count, " +
            "(SELECT count(1) from tb_chat_friend  where uid=#{uid} and is_liked=1) as my_like_count, " +
            "(SELECT count(1) from tb_chat_friend  where target_uid=#{uid} and state=1) as attent_me_count, " +
            "(SELECT count(1) from tb_chat_friend  where target_uid=#{uid} and is_liked=1) as like_me_count ")
    StatLikeAndAttentUserEntity statLikeAndAttent(@Param("uid") Integer uid);

    @Select("select * from tb_chat_friend where uid=#{uid} and target_uid=#{targetUid}")
    ChatFriendEntity findById(@Param("uid") Integer uid,@Param("targetUid") Integer targetUid);

}
